import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;


public class Board extends JPanel implements ActionListener
{
	Image img;
	Player player;
	Timer time;
	Flying fly;
	
	public Board()
	{
		img = new ImageIcon("images/background.gif").getImage();
		player = new Player();
		fly = new Flying();
		
		addKeyListener(new KL());
		ML mouselis = new ML();
		addMouseListener(mouselis);
		addMouseMotionListener(mouselis);
		setFocusable(true);
		
		time = new Timer(5, this);
		time.start();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		player.move();
		fly.move();
		repaint();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.drawImage(img, 0, 0, null);
		
//		System.out.println("direction=" + player.getDirection());
		g2d.drawImage(player.getImage(), player.getX(), player.getY(), null);
		
		fly.drawBullets(g);

	}
	
	
	private class ML extends MouseAdapter implements MouseListener, MouseMotionListener
	{
		
		public void mouseEntered(MouseEvent e)
		{
			System.out.println("Mouse has Entered.");
			
		}
		public void mouseMoved(MouseEvent e)
		{
			System.out.println("Mouse Has Moved.");
			player.setDirection(e.getPoint().getX() , e.getPoint().getY());
			
		}
		public void mouseClicked(MouseEvent e)
		{
			
			System.out.println("You Have Clicked Your Mouse.");
		}
		public void mouseDragged(MouseEvent e)
		{
	//		System.out.println("Mouse is being Dragged");
			mousePressed(e);
			
		}
		public void mousePressed(MouseEvent e)
		{
			fly.addBullet(new Bullet(player.getX(), player.getY(), e.getPoint().getX(), e.getPoint().getY()));
			System.out.println("Mouse is pressed");
		}
	}
	
	private class KL extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			switch(e.getKeyCode())
			{
				case KeyEvent.VK_UP:
					player.setYVelocity(-1);
					break;
				case KeyEvent.VK_DOWN:
					player.setYVelocity(1);
					break;
				case KeyEvent.VK_LEFT:
					player.setXVelocity(-1);
					break;
				case KeyEvent.VK_RIGHT:
					player.setXVelocity(1);
					break;					
			}
		}
		
		public void keyReleased(KeyEvent e)
		{
			switch(e.getKeyCode())
			{
				case KeyEvent.VK_UP:
					player.setYVelocity(0);
					break;
				case KeyEvent.VK_DOWN:
					player.setYVelocity(0);
					break;
				case KeyEvent.VK_LEFT:
					player.setXVelocity(0);
					break;
				case KeyEvent.VK_RIGHT:
					player.setXVelocity(0);
					break;					
			}
		}
	}
}
